package com.jie.other

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.StringUtils
import com.jie.commonlib.base.BaseActivity
import com.jie.commonlib.support.arouter.ARouterActivity
import com.jie.commonlib.support.arouter.ARouterParamKey
import com.jie.commonlib.support.eventbus.Event
import com.jie.commonlib.support.eventbus.EventBusManager
import com.jie.commonlib.support.eventbus.EventConstant
import com.jie.other.aidl.AidlActivity
import com.jie.other.bean.ui.OtherHomeBean
import com.jie.other.compose.Study01Activity
import com.jie.other.compose.Study02Activity
import com.jie.other.compose.Study03Activity
import com.jie.other.compose.Study04Activity
import com.jie.other.jetpack.DataBindingActivity
import com.jie.other.jetpack.LifecycleActivity
import com.jie.other.jetpack.LiveDataActivity
import com.jie.other.jetpack.ViewModelActivity
import com.jie.other.jetpack.flow.FlowTestActivity
import com.jie.other.jetpack.paging.PagingActivity
import com.jie.other.jetpack.room.RoomActivity
import com.jie.other.network.NetWorkActivity
import com.jie.other.ui.list_choose_dialog.ListChooseDialog
import com.jie.other.ui.list_choose_dialog.ListItemDTO
import com.jie.other.ui.theme.OtherMaterialTheme
import com.jie.other.ui.widget.CommonTopAppBar

/**
 * 其他测试主界面
 */
@Route(path = ARouterActivity.ACTIVITY_OTHER_HOME)
class OtherHomeActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            val fromApp = intent?.extras?.getBoolean(ARouterParamKey.PARAM_FROM_APP) ?: false
            OtherMaterialTheme(
                isNewColor = fromApp
            ) {
                Surface {
                    Page(
                        mData,
                        backClick = {
                            finish()
                        },
                        itemClick = { item : OtherHomeBean ->
                            if (item.activity == Study01Activity::class.java){
                                EventBusManager.getInstance().sendStickyEvent(
                                    Event(EventConstant.CODE_FROM_APP, fromApp)
                                )
                            }
                            if (item.activity == PagingActivity::class.java){
                                showDataSourceChooseDialog(item.activity)
                            } else {
                                startActivity(Intent(this, item.activity))
                            }
                        },
                    )
                }
            }
        }
    }

    /**
     * 显示一个列表选择Dialog
     */
    private fun showDataSourceChooseDialog(activity: Class<PagingActivity>) {
        ListChooseDialog()
            .apply {
                setData(
                    StringUtils.getString(R.string.other_data_form_http),
                    StringUtils.getString(R.string.other_data_form_database)
                )
//                setData(mutableListOf<ListItemDTO>().apply {
//                    for (i in 0..30){
//                        add(ListItemDTO("$i", "列表项 + $i"))
//                    }
//                })
                mItemClickListener = { pos, _ ->
                    startActivity(Intent(this@OtherHomeActivity, activity).apply {
                        putExtra(PagingActivity.PARAM_DATA_SOURCE, pos == 0)
                    })
                }
            }
            .show(supportFragmentManager, ListChooseDialog::class.java.name)
    }

    companion object {
        val mData : ArrayList<OtherHomeBean> =
            arrayListOf(
                OtherHomeBean(R.string.other_network, NetWorkActivity::class.java),
                OtherHomeBean(R.string.other_lifecycle, LifecycleActivity::class.java),
                OtherHomeBean(R.string.other_flow, FlowTestActivity::class.java),
                OtherHomeBean(R.string.other_live_data, LiveDataActivity::class.java),
                OtherHomeBean(R.string.other_view_model, ViewModelActivity::class.java),
                OtherHomeBean(R.string.other_data_binding, DataBindingActivity::class.java),
                OtherHomeBean(R.string.other_room, RoomActivity::class.java),
                OtherHomeBean(R.string.other_paging, PagingActivity::class.java),
                OtherHomeBean(R.string.other_compose_study_01, Study01Activity::class.java),
                OtherHomeBean(R.string.other_compose_study_02, Study02Activity::class.java),
                OtherHomeBean(R.string.other_compose_study_03, Study03Activity::class.java),
                OtherHomeBean(R.string.other_compose_study_04, Study04Activity::class.java),
                OtherHomeBean(R.string.other_aidl, AidlActivity::class.java),
            )
    }
}

@Composable
fun Page(
    menuList: List<OtherHomeBean>,
    backClick : () -> Unit = {},
    itemClick: (OtherHomeBean) -> Unit = {}) {
    Scaffold(
        topBar = {
            CommonTopAppBar(
                title = stringResource(id = R.string.other_app_name),
                backClick = backClick
            )
        },
        content = {
            MenuList(
                menuList = menuList,
                modifier = Modifier
                    .padding(top = it.calculateTopPadding())
                    .background(color = Color.Yellow),
                itemClick = itemClick,
            )
        }
    )
}

@Composable
fun MenuList(menuList: List<OtherHomeBean>,
             modifier: Modifier = Modifier,
             itemClick: (OtherHomeBean) -> Unit = {}) {
    Surface(modifier = modifier) {
        LazyColumn {
            items(menuList) { item ->
                ItemCard(item, itemClick)
            }
        }
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ItemCard(item: OtherHomeBean, itemClick: (OtherHomeBean) -> Unit = {}) {
    Column(modifier = Modifier.padding(horizontal = 10.dp, vertical = 3.dp)) {
        Card(
            onClick = {
                itemClick.invoke(item)
            },
        ) {
            Text(
                text = stringResource(id = item.name),
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp),
                fontStyle = FontStyle.Normal
            )
        }
    }
}

@Preview
@Composable
fun PreviewPage() {
    Page(OtherHomeActivity.mData)
}